<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/10/30 17:59
 * Intro:
 */

namespace addons\podunk\controller;


use addons\podunk\library\Curl;
use think\Db;

class Test extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    public function hello(){
        $taskChr = '';
        $content = '{"task_name":"4545","app":"tiktok_store","task":"brush","open_live_mode":"tiktok_id","open_live_text":"1365244366","product_name":"我的","start_time":1605867507,"uuid":"627118b5-f634-a398-d926-72e7d15f9984"}';
        $content = json_encode($content);
        echo str_replace('\\', '',$content);exit;
        for ($i=0; $i<=strlen($content); $i++){
            if($this->isChineseChr(substr($content,$i,1))){
                echo substr($content,$i,1);
            }
            if(!$this->isChineseChr(substr($content,$i,1))){
                $taskChr = $taskChr.substr($content,$i,1);
            }else{
                $taskChr = $taskChr.$this->unicode_encode(substr($content,$i,1));
                echo 1;
            }
        }
        echo $taskChr;
    }

    public function createUser()
    {
        $username = $this->getRandomString(6);
        $password = $this->getRandomString(8);
        $email = $this->getRandomString(6).'@qq.com';
        $mobile = $this->randomMobile(1);

        $ret = $this->auth->register($username, $password, $email, $mobile, ['parent_id'=>1,'pfull'=>'-1,1']);
    }

    function getRandomString($len, $chars=null)
    {
        if (is_null($chars)) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

    //随机生成n条手机号
    protected function randomMobile()
    {
        $tel_arr = array(
            '130','131','132','133','134','135','136','137','138','139','144','147','150','151','152','153','155','156','157','158','159','176','177','178','180','181','182','183','184','185','186','187','188','189',
        );
        return $tel_arr[array_rand($tel_arr)].mt_rand(1000,9999).mt_rand(1000,9999);
    }


    function unicode_encode($strLong) {
        $strArr = preg_split('/(?<!^)(?!$)/u', $strLong);//拆分字符串为数组(含中文字符)
        $resUnicode = '';
        foreach ($strArr as $str)
        {
            $bin_str = '';
            $arr = is_array($str) ? $str : str_split($str);//获取字符内部数组表示,此时$arr应类似array(228, 189, 160)
            foreach ($arr as $value)
            {
                $bin_str .= decbin(ord($value));//转成数字再转成二进制字符串,$bin_str应类似111001001011110110100000,如果是汉字"你"
            }
            $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bin_str);//正则截取, $bin_str应类似0100111101100000,如果是汉字"你"
            $unicode = dechex(bindec($bin_str));//返回unicode十六进制
            $_sup = '';
            for ($i = 0; $i < 4 - strlen($unicode); $i++)
            {
                $_sup .= '0';//补位高字节 0
            }
            $str = '\\u' . $_sup . $unicode; //加上 \u 返回
            $resUnicode .= $str;
        }
        return $resUnicode;
    }

    /**
     * 判断是否都是中文
     * @param $str
     * @return int
     */
    public function isChineseChr($str)
    {
        $len = preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str);
        if ($len)
        {
            return true;
        }
        return false;
    }



}